<!doctype html>
<html>
	<head>
		<meta charset="utf-8">
		<title>登录页面</title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no,viewport-fit=cover" />
		<link href="css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" type="text/css" href="css/login.css" />
		<script src="js/jquery.js"></script>
		<script src="js/mui.min.js"></script>

	</head>
	<body>
		<div class="main">
			<div class="mainTop">
				<img src="images/logo_03.png">
				<p>
					<b>撬装·工业</b>
					<span>撬装处理设备远程监控系统</span>
				</p>
			</div>
			<div class="mainCenter">
				<div class="mui-input-row mainLine">
					<label>账号</label>
					<input type="text" class="login_phone">
				</div>
				<div class="mui-input-row mainLine">
					<label>密码</label>
					<input type="password" class="login_password">
				</div>
				<p class="lpw login_foget">忘记密码？</p>

			</div>
			<div class="mainBottom">
				<button type="button" class="mui-btn mui-btn-blue login_btn">登录</button>
				<p class="reg login_register">新用户？<a>注册</a></p>
			</div>
		</div>



		<script type="text/javascript">
			mui.init();
			var _url = "http://112.54.80.235:8038";
			/* 数据 */
			mui.plusReady(function() {
				//去掉角标
				plus.runtime.setBadgeNumber(0);
				//推送标识
				var cid = "";
				var cidtime = setInterval(function() {
					var pinf = plus.push.getClientInfo();
					cid = pinf.clientid; //客户端标识 
					if (!!cid) {
						clearInterval(cidtime);
					}
				}, 2000)

				//关闭等待框
				plus.nativeUI.closeWaiting();
				var login_state = localStorage.getItem("login_state");
				//检查更新
				plus.runtime.getProperty(plus.runtime.appid, function(inf) {
					checkUpdate(inf.version);
				});
				//点击登录
				mui(document).on('tap', '.login_btn', function(e) {
					console.log(cid);
					var phone = mui('.login_phone')[0].value;
					var password = mui('.login_password')[0].value;
					if (phone.length < 1) {
						mui.toast('请输入账号');
						return false;
					};
					if (password.length < 1) {
						mui.toast('请输入密码');
						return false;
					};
					//等待
					plus.nativeUI.showWaiting();

					$.ajax({
						type: 'post',
						url: _url + '/actionapi/Users/YongHuDengLu',
						dataType: 'json',
						contentType: "application/json; charset=utf-8",
						timeout: 5000,
						data: JSON.stringify({
							phone: phone,
							pwd: password,
							ClientId: cid
						}),
						success: function(data) {
							console.log(data.userinfo);
							plus.nativeUI.closeWaiting(); //等待框关闭
							if (data.Code == 1) {
								mui.toast('登录成功！');
								//储存登录状态
								localStorage.setItem("login_state", 1);
								localStorage.setItem("user_phone",data.userinfo.Phone);
								localStorage.setItem("eqno",data.userinfo.Device);
								mui.openWindow({
									url: 'main.html',
									id: 'main.html',
									waiting: {
										autoShow: true, //自动显示等待框，默认为true
										title: '正在加载...', //等待对话框上显示的提示内容
									}
								});
								// var main = plus.webview.getWebviewById('main.html');
								// mui.fire(main, 'gohome');
							} else {
								mui.toast(data.message);
							}
						},
						error: function(XMLHttpRequest, textStatus, errorThrown) {
							plus.nativeUI.closeWaiting(); //等待框关闭
							mui.toast('YongHuDengLu接口失败！');
							console.log(JSON.stringify(XMLHttpRequest));
						}
					})
				});
				//忘记密码
				mui(document.body).on('tap', '.login_foget', function(e) {
					mui.openWindow({
						url: 'password.html',
						id: 'password.html',
						waiting: {
							autoShow: true, //自动显示等待框，默认为true
							title: '正在加载...', //等待对话框上显示的提示内容
						}
					})
				});
				//注册
				mui(document.body).on('tap', '.login_register a', function(e) {
					mui.openWindow({
						url: 'register.html',
						id: 'register.html',
						waiting: {
							autoShow: true, //自动显示等待框，默认为true
							title: '正在加载...', //等待对话框上显示的提示内容
						}
					})
				});
				//需要传递当前的版本
				function checkUpdate(wgtVer) {
					console.log(wgtVer);
					plus.nativeUI.showWaiting("检测更新...");
					$.ajax({
						url: _url + '/actionapi/Users/GetVersionInformation',
						type: 'get',
						data: {},
						success: function(json) {
							plus.nativeUI.closeWaiting();
							console.log("aa--" + JSON.stringify(json))
							var data = json.data[0]
							//数据获取
							if (json.Code == 1) {
								//是否需要更新
								if (parseFloat(data.IOSEdition) >= parseFloat(wgtVer) && mui.os.ios) {
									//苹果更新
									plus.nativeUI.confirm("检查到Ios版本有最新更新，下载升级？",
										function(event) {
											if (event.index == 0) {
												//ios下载
												var iosUrl = data.Ios // HelloH5应用在appstore的地址
												plus.runtime.openURL(iosUrl);
											}
										}, '系统消息', ["确定", "取消"],
									);
								} else if (parseFloat(data.Edition) >= parseFloat(wgtVer) && !mui.os.ios) {
									//安卓更新
									plus.nativeUI.confirm("检查到Android版本有最新更新，下载升级？",
										function(event) {
											if (event.index == 0) {
												var andurl = data.Android; // 安卓下载文件地址
												var dtask = plus.downloader.createDownload(andurl, {}, function(d, status) {
													if (status == 200) { // 下载成功
														var path = d.filename;
														plus.runtime.install(path); // 安装下载的apk文件
													} else { //下载失败
														plus.nativeUI.toast('下载失败！');
													}
												});
												dtask.start();
											}
										}, '系统消息', ["确定", "取消"],
									);
								} else {
									//判断是否登录过
									if (login_state == 1) {
										//已登录过
										mui.openWindow({
											url: 'main.html',
											id: 'main.html',
											waiting: {
												autoShow: true, //自动显示等待框，默认为true
												title: '正在加载...', //等待对话框上显示的提示内容
											}
										})
										var main = plus.webview.getWebviewById('main.html');
										mui.fire(main, 'gohome');
									};
								}
							} else {
								//获取数据失败
								plus.nativeUI.toast('获取数据失败！');
							}
						},
						error: function(xhr, error) {
							plus.nativeUI.closeWaiting();
							plus.nativeUI.toast('检测更新失败！');
						}
					});
				}
				// 下载wgt文件

				function downWgt(wgtUrl) {
					plus.nativeUI.showWaiting("下载更新文件...");
					plus.downloader.createDownload(wgtUrl, {
						filename: "_doc/update/"
					}, function(d, status) {

						if (status == 200) {
							//console.log("下载wgt成功："+d.filename);
							installWgt(d.filename); // 安装wgt包
						} else {
							//console.log("下载wgt失败！");
							plus.nativeUI.alert("下载更新失败！");
						}
						plus.nativeUI.closeWaiting();
					}).start();
				}
				// 更新应用资源  
				function installWgt(path) {
					plus.nativeUI.showWaiting("安装更新文件...");
					plus.runtime.install(path, {}, function() {
						plus.nativeUI.closeWaiting();
						plus.nativeUI.alert("应用资源更新完成！", function() {
							plus.runtime.restart();
						});
					}, function(e) {
						plus.nativeUI.closeWaiting();
						plus.nativeUI.alert("安装更新文件失败[" + e.code + "]：" + e.message);
					});
				}
			})
		</script>
	</body>
</html>
